package com.mall.service.ps;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mall.client.ps.PsTaskService;
import com.mall.custom.dao.read.PsTaskReadMapper;
import com.mall.custom.dao.write.PsTaskWriteMapper;
import com.mall.pojo.PsTask;
import com.mall.pojo.PsTaskExample;
import com.mall.pojo.PsTaskExample.Criteria;

@Service
public class PsTaskServiceImpl implements PsTaskService {
	
	@Autowired
	private PsTaskWriteMapper psTaskWriteMapper;
	@Autowired
	private PsTaskReadMapper psTaskReadMapper;
	
	@Override
	public int insertPsTask(PsTask psTask) {
		psTask.setGmtCreate(new Date());
		psTask.setGmtModify(new Date());
		return this.psTaskWriteMapper.insert(psTask);
	}

	@Override
	public List<PsTask> getPsTaskListByExample(PsTaskExample example) {
		
		return this.psTaskReadMapper.selectByExample(example);
	}

	@Override
	public PsTask getPsTaskByIdAndVersion(Long psTaskId, int version) {
		if (version <= 0  ) {
			version = 1;
		}
		PsTaskExample example = new PsTaskExample();
		Criteria criteria = example.createCriteria();
		criteria.andLastVersionEqualTo(version);
		criteria.andIdEqualTo(psTaskId);
		List<PsTask> psTaskList = this.psTaskReadMapper.selectByExample(example);
		PsTask psTask = null;
		if (psTaskList.size()>0 && psTaskList != null) {
			psTask = psTaskList.get(0);
		}
		return psTask;
	}

	@Override
	public boolean updateByIdAndVersion(Map<String, Object> psTaskMap) {
		return  this.psTaskWriteMapper.updatePsTaskByIdAndVersion(psTaskMap) >0;
	}

	@Override
	public Integer updatePsTask(PsTask psTask) {
		psTask.setGmtModify(new Date());
		return this.psTaskWriteMapper.updateByPrimaryKey(psTask);
	}

	@Override
	public int getNotFinishTaskCountByexample(PsTaskExample exampleCount) {
		return this.psTaskReadMapper.countByExample(exampleCount);
	}

	@Override
	public PsTask getPsTaskById(Long psTaskId) {
		return this.psTaskReadMapper.selectByPrimaryKey(psTaskId);
	}

	@Override
	public Integer getCountByExample(PsTaskExample example) {
		return this.psTaskReadMapper.countByExample(example);
	}

	@Override
	public Integer getTaskSumByExample(PsTaskExample example) {
		
		return this.psTaskReadMapper.getTaskSumByExample(example);
	}

	@Override
	public PsTask getPsTaskByIpAndStatusAndNum(String psuuid, int statusNew, Integer taskNum,String uuid) {
		PsTaskExample example = new PsTaskExample();
		Criteria criteria = example.createCriteria();
		if (StringUtils.isNotBlank(psuuid)) {
			criteria.andTitleEqualTo(psuuid);
		}
		
		criteria.andStatusEqualTo(statusNew);
		criteria.andTaskNumEqualTo(taskNum);
		criteria.andUuidEqualTo(uuid);
		List<PsTask> psTaskList = this.psTaskReadMapper.selectByExample(example);
		PsTask psTask = null;
		if (psTaskList.size()>0 && psTaskList != null) {
			psTask = psTaskList.get(0);
		}
		return psTask;
	}

	@Override
	public PsTask getPsTaskByUuid(String uuid) {
		PsTaskExample example = new PsTaskExample();
		Criteria criteria = example.createCriteria();
		criteria.andUuidEqualTo(uuid);
		List<PsTask> psTaskList = this.psTaskReadMapper.selectByExample(example);
		PsTask psTask = null;
		if (psTaskList.size()>0 && psTaskList != null) {
			psTask = psTaskList.get(0);
		}
		return psTask;
	}

}
